﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._02.BUS;
using HotelManagement._04.OBJECT;

namespace HotelManagement._01.FORM
{
    public partial class F22_CustomerInfo : Form
    {
        CustomerManagerBUS m_CustomerManagerBus;
        public F22_CustomerInfo()
        {
            InitializeComponent();
            init();
            LoadData();
        }
        private void init()
        {
            m_CustomerManagerBus = new CustomerManagerBUS();
        }
        private void LoadData()
        {
            m_dgv_CustomerInf.DataSource = m_CustomerManagerBus.ViewAll();
            m_cb_CustomerType.DataSource = m_CustomerManagerBus.ViewAllCustomerType();
            m_cb_CustomerType.DisplayMember = "CustomerTypeName";
            m_cb_CustomerType.ValueMember = "CustomerTypeID";
            m_cbb_Gender.SelectedItem = "Nam";
        }

        private void m_dgv_CustomerInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            m_txt_Address.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Address"].Value.ToString();
            m_date_Birthday.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Birthday"].Value.ToString();
            m_txt_CustomerID.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["CustomerID"].Value.ToString();
            m_txt_CustomerName.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["CustomerName"].Value.ToString();
           // m_txt_CustomerType.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["CustomerTypeID"].Value.ToString();
            m_cb_CustomerType.SelectedValue = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["CustomerTypeID"].Value;
            m_cbb_Gender.SelectedItem = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Gender"].Value.ToString();
            m_txt_Email.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Email"].Value.ToString();
            m_txt_IDCardNumber.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["IDCardNo"].Value.ToString();
            m_txt_National.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Nationality"].Value.ToString();
            m_txt_Note.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Note"].Value.ToString();
            m_txt_Passport.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["PassportNo"].Value.ToString();
            m_txt_Phone.Text = m_dgv_CustomerInf.Rows[m_dgv_CustomerInf.CurrentCell.RowIndex].Cells["Phone"].Value.ToString();

        }
        private Customer GetDataFromForm()
        {
            Customer v_Customer = new Customer();
            if (m_txt_CustomerID.Text != "") v_Customer.CustomerID = int.Parse(m_txt_CustomerID.Text);
            if (CValidate.IsNullString(m_txt_CustomerName.Text)) { MessageBox.Show("Chưa nhập tên khách hàng !"); return null; }
            if (CValidate.IsNullString(m_txt_National.Text)) { MessageBox.Show("Chưa nhập quốc tịch !"); return null; }          
            if (CValidate.IsNullString(m_txt_Address.Text)) { MessageBox.Show("Chưa nhập địa chỉ !"); return null; }
            if (!CValidate.IsNullString(m_txt_Email.Text))
            {
                if (!CValidate.IsEmail(m_txt_Email.Text)) { MessageBox.Show("Email không đúng !"); return null; }
            }
            if (!CValidate.IsNullString(m_txt_IDCardNumber.Text))
            {
                if (CValidate.IsString(m_txt_IDCardNumber.Text)) { MessageBox.Show("Mã số thanh toán phải là số !"); return null; }
            }

            if (CValidate.IsString(m_txt_Passport.Text)|| !CValidate.IsPassport(m_txt_Passport.Text)) { MessageBox.Show("Chưa nhập hoặc nhập số hộ chiếu / CMND không đúng !"); return null; }
            if (!CValidate.IsNumber(m_txt_Phone.Text)) { MessageBox.Show("Chưa nhập hoặc nhập số điện thoại không đúng !"); return null; }
            TimeSpan v_timeSpan=DateTime.Now - m_date_Birthday.Value;
            if (v_timeSpan.Days < 16*365 || v_timeSpan.Days > 120*365) { MessageBox.Show("Hãy nhập đúng tuổi, khách thuê phòng > 15 tuổi và <120 tuổi !"); return null; }
            v_Customer.Address = m_txt_Address.Text;
            v_Customer.Birthday = DateTime.Parse(m_date_Birthday.Text);
            v_Customer.CustomerName = m_txt_CustomerName.Text;
            v_Customer.Email = m_txt_Email.Text;
            v_Customer.Gender = m_cbb_Gender.SelectedItem.ToString();
            v_Customer.IDCardNo = m_txt_IDCardNumber.Text;
            v_Customer.Nationality = m_txt_National.Text; 
            v_Customer.Note = m_txt_Note.Text;
            v_Customer.PassportNo = m_txt_Passport.Text;
            v_Customer.Phone = m_txt_Phone.Text; 
            v_Customer.CustomerTypeID =int.Parse( m_cb_CustomerType.SelectedValue.ToString());
            return v_Customer;
        }

        private void update_Click(object sender, EventArgs e)
        {
                if (m_txt_CustomerID.Text == "") return;
                if (GetDataFromForm() == null) return; 
                Customer customer = new Customer();
                customer = GetDataFromForm();
                if (!m_CustomerManagerBus.UpdateCustomer(customer)) { MessageBox.Show("Cập nhật dữ liệu không thành công"); return; }
                LoadData();
                MessageBox.Show("Cập nhật thành công");
                ResetText();
            
        }

        private void add_Click(object sender, EventArgs e)
        {
                if (m_txt_CustomerID.Text != "") return;
                if (GetDataFromForm() == null) return;
                Customer customer = new Customer();
                customer = GetDataFromForm();
                m_CustomerManagerBus.InsertCustomer(customer);
                LoadData();
                MessageBox.Show("Thêm khách hàng thành công.");
                ResetText();

        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (m_txt_CustomerID.Text == "")
            {
                MessageBox.Show("Bạn chưa chọn khách hàng nào để xóa");
            }
            else
            {
                Customer customer = new Customer();
                customer.CustomerID = int.Parse(m_txt_CustomerID.Text);
                //
                            
                DialogResult OK;
                OK = MessageBox.Show("Bạn muốn xóa khách hàng đã chọn ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (OK == DialogResult.Yes)
                {
                    if (!m_CustomerManagerBus.DeleteCustomer(customer)) return;
                    LoadData();
                    MessageBox.Show("Xóa thành công");
                    ResetText();
                }

            }
        }

        private void search_Click(object sender, EventArgs e)
        {

            Customer customer = new Customer();
            //customer.Address = m_txt_Address.Text;
            //customer.Birthday = DateTime.Parse(m_date_Birthday.Text);
            //customer.CustomerID = int.Parse(m_txt_CustomerID.Text);
            customer.CustomerName = m_txt_CustomerName.Text;
            //customer.Email = m_txt_Address.Text;
            //customer.Gender = m_cbb_Gender.SelectedItem.ToString();
            //customer.IDCardNo = m_txt_IDCardNumber.Text;
            //customer.Nationality = m_txt_National.Text;
            //customer.Note = m_txt_Note.Text;
            //customer.PassportNo = m_txt_Passport.Text;
            //customer.Phone = m_txt_Phone.Text;
            //customer.CustomerTypeID = m_cb_CustomerType.SelectedIndex + 1;
            m_dgv_CustomerInf.DataSource = m_CustomerManagerBus.Search(customer);
        }
        private void ResetText()
        {
            m_txt_Address.Text = "";
            m_txt_CustomerID.Text = "";
            m_txt_CustomerName.Text = "";
            m_txt_Email.Text = "";
            m_cbb_Gender.SelectedItem = "Nam";
            m_txt_National.Text = "";
            m_txt_Note.Text = "";
            m_txt_Passport.Text = "";
            m_txt_Phone.Text = "";
            m_txt_IDCardNumber.Text = "";
            m_date_Birthday.Value = DateTime.Now;
        }
        private void reset_Click(object sender, EventArgs e)
        {
            ResetText();
            LoadData();
        }
    }
}
